﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using EntityObject;
using DataAccessObject;
using System.Data;
using ThangNM_LIB.FUNC;

namespace FacebookApp
{
    public partial class Facebook_List : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            MsgUC1.setClearMsg();
            if (!IsPostBack)
            {
            }
        }

        protected void btnShow_Click(object sender, EventArgs e)
        {
            try
            {
                ddlPage.Visible = false;
                pnlDetail.Visible = false;
                DataTable dt = (new Option_EXDAO()).SelectBy_object_id(new CommentEO(Convert.ToInt64(txtUserID.Text), 0, 0, 0));
                if (CommonsCheck.DataTable_OK(dt))
                {
                    ddlPage.DataSource = dt;
                    ddlPage.DataValueField = "id";
                    ddlPage.DataTextField = "name";
                    ddlPage.DataBind();
                    ddlPage.Visible = true;
                    Int64 page_id = 0;
                    Int64.TryParse(ddlPage.SelectedValue, out page_id);
                    if (BindData(grvPost, new CommentEO(0, page_id, 0, 0)))
                        pnlDetail.Visible = true;
                    else
                        pnlDetail.Visible = false;
                }
            }
            catch (Exception ex)
            {
                MsgUC1.setNotifications(ConstEx.FunctionSetMsg.ERROR, ex);
            }
        }

        protected Boolean BindData(GridView grvList, CommentEO objInput)
        {
            try
            {
                MsgUC1.setClearMsg();
                DataTable dt = (new Option_EXDAO()).SelectBy_object_id(objInput);
                if (CommonsCheck.DataTable_OK(dt))
                {
                    grvList.DataSource = dt;
                    grvList.DataBind();
                    return true;
                }
            }
            catch (Exception ex)
            {
                MsgUC1.setNotifications(ConstEx.FunctionSetMsg.ERROR, ex);
            }
            return false;
        }

        protected void grvPost_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grvPost.PageIndex = e.NewPageIndex;
            Int64 page_id = 0;
            Int64.TryParse(ddlPage.SelectedValue, out page_id);
            BindData(grvPost, new CommentEO(0, page_id, 0, 0));
        }

        protected void grvPost_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Int64 post_id = Convert.ToInt64(grvPost.DataKeys[e.Row.RowIndex].Value);
                GridView grvComment = e.Row.FindControl("grvComment") as GridView;
                Image imgComment_Show_Hide = e.Row.FindControl("imgComment_Show_Hide") as Image;
                if (BindData(grvComment, new CommentEO(0, 0, post_id, 0)))
                    imgComment_Show_Hide.Visible = true;
                else
                    imgComment_Show_Hide.Visible = false;
            }
        }

        protected void grvComment_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                GridView grvComment = (GridView)sender;
                //if (String.IsNullOrEmpty(lblMsg.Text))
                //    lblMsg.Text = e.Row.RowIndex.ToString();
                Int64 comment_id = Convert.ToInt64(grvComment.DataKeys[e.Row.RowIndex].Value);
                GridView grvReply = e.Row.FindControl("grvReply") as GridView;
                Image imgReply_Show_Hide = e.Row.FindControl("imgReply_Show_Hide") as Image;
                if (BindData(grvReply, new CommentEO(0, 0, 0, comment_id)))
                    imgReply_Show_Hide.Visible = true;
                else
                    imgReply_Show_Hide.Visible = false;
            }
        }

        protected void ddlPage_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (BindData(grvPost, new CommentEO(0, Convert.ToInt64(ddlPage.SelectedValue), 0, 0)))
                pnlDetail.Visible = true;
            else
                pnlDetail.Visible = false;
        }
    }
}